
package com.xiaoma.myaudience.util.app;

import com.xiaoma.myaudience.util.Logger;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;

import java.lang.Thread.UncaughtExceptionHandler;

public class BaseApplication extends Application {
    private static final HandlerThread sWorkerThread = new HandlerThread("BaseApplication");
    static {
        sWorkerThread.start();
    }

    private static final Handler sWorker = new Handler(sWorkerThread.getLooper());


    @Override
    public void onCreate() {
        super.onCreate();
//        BaseException.init(this);
    }


    /**
     * 执行线程任务.
     */
    public void postExe(Runnable run) {
        sWorker.post(run);
    }
    
    /**
     * 当程序异常退出时调用<br/>
     * s
     * @param e
     */
    public void onExceptionExit(Throwable exception) {
        
    }
    
    public static class BaseException implements UncaughtExceptionHandler {
        private static BaseApplication sApp;
        private static Thread.UncaughtExceptionHandler sDefaultExceptionHandler = Thread
                .getDefaultUncaughtExceptionHandler();

        private static BaseException sCustomException = new BaseException();

        @Override
        public void uncaughtException(Thread thread, Throwable exception) {
            sApp.onExceptionExit(exception);
            sDefaultExceptionHandler.uncaughtException(thread, exception);
        }

        public static void init(BaseApplication app) {
            sApp = app;
            Thread.setDefaultUncaughtExceptionHandler(sCustomException);
        }
    }

}
